'use strict';
const Controller = require('egg').Controller;
class ChapterController extends Controller {
    async getInfo() {
        const { ctx } = this;
        const book_id = this.ctx.query.bookId,
        zhangjie = this.ctx.query.zhangjie,
            data1 = `SELECT * FROM book WHERE bookID = ${book_id}`,
            data2 = `SELECT * FROM chapter WHERE bookID =${book_id} and zhangjie = ${zhangjie}`,
            result1 = await this.app.mysql.query(data1),
            result2 = await this.app.mysql.query(data2)
        let getMax = `select * from chapter WHERE bookID = ${book_id} ORDER BY zhangjie DESC`,
            result3 = await this.app.mysql.query(getMax),
            max = result3[0].zhangjie
        ctx.body = [result1, result2, max]

    }

    async whetherlogin() {
        const { ctx } = this;
        const el = this.ctx.session.email
        if (el) {
            ctx.body = { code: 200, el }
        } else {
            ctx.body = { code: 404 }
        }
    }
    async InsertInfo() {
        const { ctx } = this;
        const b_id = this.ctx.query.n1,
            el = this.ctx.query.n2,
            flag = this.ctx.query.n3
        if (flag == 'false') {
            let info = `insert into follow_books (book_id,user_email) VALUES ('${b_id}','${el}')`,
                res = await this.app.mysql.query(info)
        } else if (flag == 'true') {
            let del = `delete from follow_books where book_id='${b_id}' and user_email='${el}'`,
                res2 = await this.app.mysql.query(del)
        }
        ctx.body = '111'

    }
}

module.exports = ChapterController;
